# -*- coding: utf-8 -*-
"""
Created on Tue May 12 14:22:17 2020

@author: Farman
"""

'''
Encoding pattern of Landsat data file name
-------------------------------------------------------------------------------
According to landsat file  LC81400072013107LGN01_sr_band1-7.tif
    L-----Landsat
    C-----OLI/TIRS combined
    8-----Landsat 8
    140---WRS path
    007---WRS row
    2013--year 2013
    107---Julian day of year
    LGN---ground station identifier
    01----archive version number
    
Refer https://www.earthdatascience.org/courses/earth-analytics/multispectral-remote-sensing-data/landsat-data-in-r-geotiff/
for more details.
'''


import datetime
import pathlib



def parse(landsat_file_name):
    '''
    Parse landsat file name.

    Parameters
    ----------
    landsat_file_name : string
        Name of a landsat file.

    Returns
    -------
    .

    '''
    landsat_file_name = str(pathlib.Path(landsat_file_name).name)
    sat_name  = landsat_file_name[0]
    sat_type  = landsat_file_name[1]
    sat_num   = landsat_file_name[2]
    wrs_path  = int(landsat_file_name[3:6])
    wrs_row   = int(landsat_file_name[6:9])
    data_year = int(landsat_file_name[9:13])
    data_yday = int(landsat_file_name[13:16]) # day of year
    
    t = datetime.date(data_year, 1, 1)
    td = datetime.timedelta(data_yday)
    t += td
    
    data_month = t.month
    data_mday  = t.day # day of month
    return sat_name, sat_type, sat_num, wrs_path, wrs_row, data_year, data_month, data_mday



def parse_day_of_year(landsat_file_name):
    '''
    Parse landsat file name.

    Parameters
    ----------
    landsat_file_name : string
        Name of a landsat file.

    Returns
    -------
    .

    '''
    landsat_file_name = str(pathlib.Path(landsat_file_name).name)
    return int(landsat_file_name[13:16]) # day of year

    

def format(sat_name, sat_type, sat_num, wrs_path, wrs_row, 
           data_year, data_month, data_mday, tail='LGN01_sr_band1-7.tif'):
    '''
    Format a landsat data file name with info given.

    Parameters
    ----------
    sat_name : string
        'L'.
    sat_type : string
        'C' 'O' 'T' 'E' or 'M'.
    sat_num : integer
        8 for landsat 8.
    wrs_path : integer
        WRS coordinate path of scene.
    wrs_row : integer
        WRS coordinate row of scene.
    data_year : integer
        Year of scene date.
    data_month : integer
        Month of scene date.
    data_mday : integer
        Day of month of scene date.
    tail : string
        DESCRIPTION.

    Returns
    -------
    Landsat data file name formatted with info given.
    '''
    t_begin   = datetime.date(data_year, 1, 1)
    t_current = datetime.date(data_year, data_month, data_mday)
    t_delta   = t_current - t_begin
    return "%s%s%s%03d%03d%04d%03d%s"%(sat_name, sat_type, sat_num, wrs_path, wrs_row, 
           data_year, t_delta.days, tail)


#landsat_file_name = 'LC81400072013107LGN01_sr_band1-7.tif'
#print(parse(landsat_file_name))
#print(format('L', 'C', '8', 100, 55, 2020, 5, 12))